package com.easy.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.core.domain.model.LoginBody;
import com.easy.user.mapper.UserMapper;
import com.easy.common.core.domain.entity.User;
import com.easy.user.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private UserMapper userMapper;

    /**
     * 通过 用户名 查询用户
     *
     * @param userName 用户名
     * @return {@link User}
     */
    @Override
    public User selectUserByUserName(String userName) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(userName), User::getUserName, userName);
        return userMapper.selectOne(lqw);
    }

    /**
     * 通过 手机号 查询用户
     *
     * @param phone 电话
     * @return {@link User}
     */
    @Override
    public User selectByUserPhone(String phone) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(phone), User::getPhone, phone);
        return userMapper.selectOne(lqw);
    }

    /**
     * 通过 电子邮件 查询用户
     *
     * @param email 电子邮件
     * @return {@link User}
     */
    @Override
    public User selectByUserEmail(String email) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(email), User::getEmail, email);
        return userMapper.selectOne(lqw);
    }

    /**
     * 检查 用户名 是否 唯一
     *
     * @param userName 用户名
     * @return {@link String}
     */
    @Override
    public boolean checkUserName(String userName) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(userName), User::getUserName, userName);
        User user = userMapper.selectOne(lqw);
        return user != null;
    }

    /**
     * 检查 用户 电话 是否 唯一
     *
     * @param userPhone 用户电话
     * @return boolean
     */
    @Override
    public boolean checkUserPhone(String userPhone) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(userPhone), User::getPhone, userPhone);
        User user = userMapper.selectOne(lqw);
        return user != null;
    }

    /**
     * 查看 用户 电子邮件 是否 唯一
     *
     * @param userEmail 用户电子邮件
     * @return boolean
     */
    @Override
    public boolean checkUserEmail(String userEmail) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNoneEmpty(userEmail), User::getEmail, userEmail);
        User user = userMapper.selectOne(lqw);
        return user != null;
    }

    /**
     * 查询用户类型
     *
     * @param user 用户
     * @return 0：用户 1：卖家
     */
    @Override
    public String selectUserType(User user) {
        System.out.println(user);
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getId, user.getId());
        wrapper.select(User::getType);
        User user1 = userMapper.selectOne(wrapper);
        return user1.getType();
    }

    /**
     * 检查 用户 密码是否正确
     *
     * @param userName 用户名
     * @param password 密码
     * @return boolean
     */
    @Override
    public boolean checkUserPassword(String userName, String password) {

///        String nowPassword = PasswordEncryptionUtils.encryption(password);

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName, userName);
        lqw.eq(User::getPassword, password);

        User user = userMapper.selectOne(lqw);

        return user != null;
    }



    /**
     * 检查 用户是否 允许操作
     *
     * @param user 登录用户
     */
    @Override
    public boolean checkUserAllowed(LoginBody user) {

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUserName, user.getUserName());
        User oneUser = userMapper.selectOne(lqw);
        if (oneUser.getStatus().equals("1")) {
            return false;
        } else if (oneUser.getDelFlag().equals("1")) {
            return false;
        }
        return true;
    }

    @Override
    public int updateUserStatus(User user) {
        return 0;
    }

    /**
     * 更新 用户 基本信息
     *
     * @param user 用户
     * @return int
     */
    @Override
    public int updateUserBasicInfo(User user) {
////        int i = ordUserMapper.updateUserBasicInfo(user);
//        LambdaQueryWrapper<OrdUser> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(StringUtils.isNoneEmpty(user.getUserName()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getPassword()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getPhone()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getEmail()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getSex()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getIdNumber()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getAvatar()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getStatus()), );
////        lqw.eq(StringUtils.isNoneEmpty(user.getCreateTime()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getCreateAddress()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getLoginLocation()), );
////        lqw.eq(StringUtils.isNoneEmpty(user.getLoginTime()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getDelFlag()), );
//        lqw.eq(StringUtils.isNoneEmpty(user.getMembersType()), );
        return 0;
    }

    /**
     * 更新用户密码
     *
     * @param userName    用户名
     * @param nowPassword 新密码
     * @return int
     */
    @Override
    public int updateUserPasswordByUserName(String userName, String nowPassword) {
        LambdaUpdateWrapper<User> lqw = new LambdaUpdateWrapper<>();
///        nowPassword = PasswordEncryptionUtils.encryption(nowPassword);
        lqw.set(User::getPassword, nowPassword);
        lqw.eq(User::getUserName, userName);
        int update = userMapper.update(null, lqw);
        return update;
    }

    /**
     * 通过电话更新用户密码
     *
     * @param phone    手机号
     * @param nowPassword 现在密码
     * @return int
     */
    @Override
    public int updateUserPasswordByPhone(String phone, String nowPassword) {
        LambdaUpdateWrapper<User> lqw = new LambdaUpdateWrapper<>();
///        nowPassword = PasswordEncryptionUtils.encryption(nowPassword);
        lqw.set(User::getPassword, nowPassword);
        lqw.eq(User::getPhone, phone);
        int update = userMapper.update(null, lqw);
        return update;
    }

    /**
     * 通过电子邮件更新用户密码
     *
     * @param email    邮箱号
     * @param nowPassword 现在密码
     * @return int
     */
    @Override
    public int updateUserPasswordByEmail(String email, String nowPassword) {
        LambdaUpdateWrapper<User> lqw = new LambdaUpdateWrapper<>();
///        nowPassword = PasswordEncryptionUtils.encryption(nowPassword);
        lqw.set(User::getPassword, nowPassword);
        lqw.eq(User::getPhone, email);
        int update = userMapper.update(null, lqw);
        return update;
    }

    @Override
    public boolean updateUserAvatar(String userName, String avatar) {
        return false;
    }
}
